page.form = {};
page.pageInit = function(){
  //初始化tree
  TreeCtr.treeInit("#sysResourcesTree");
  //初始化grid
  GirdCtr.gridInit();
  GirdCtr.buttonBind()
  ButtonGirdCtr.gridInit();
  ButtonGirdCtr.buttonBind()
}

var TreeCtr = $.extend({},defaultZtreeConFig,{
	setting:{
		view: {
			selectedMulti: false
		},
		async: {
			enable: true,
			url:contextPath+"/treeController/menuTree.htm",
			autoParam:["id", "name=n", "level=lv"],
			otherParam:{"otherParam":"zTreeAsyncTest"},
		    dataType:"json"
		},
		callback: {
			onClick: function(e,id,node){
			   //主GIRD刷新
			   GirdCtr.gridQuery({parentMenuId:node.id})
			   ButtonGirdCtr.gridQuery({menuId:"nolist"})
			}
		}
	}
});

//grid控制器
var GirdCtr =  $.extend({},defaultGridConFig,{
   gridInit: function(){
	 var _this = this;
	//创建jqGrid组件
	_this.grid = $("#menuGrid").jqGrid({
		 			datatype: "json",
		 			//postData:_this.queryParams(),
		 			url : contextPath+'/system/resources/query.htm',//组件创建完成之后请求数据的url
					datatype : "json",//请求数据返回的类型。可选json,xml,txt
					colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
					             {label:"主键", name : "menuId",index:"menu_id",hidden:true,key:true},
					             {label:"菜单名称", name : 'menuName',index : 'menu_name',width : 140,align : "center"}, 
					             {label:"样式", name : 'icon',index : 'icon',width : 100,align : "center"}, 
					             // {label:"上级菜单", name : 'parentMenuId',index : 'parentMenuId',width : 120,align : "center",}, 
					             {label:"类型", name : 'menuType',index : 'menu_Type',width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.menuType)}}, 
					             {label:"路径", name : 'url',index : 'url',width : 220,align : "center"}, 
					             {label:"权限路径", name : 'authUrl',index : 'auth_Url',width : 160,align : "center"}, 
					             {label:"排序", name : 'indexSort',index : 'index_Sort',width : 50,align : "center"} ,
					             {label:"创建时间", name : 'createTime',index : 'create_Time',width : 130,align : "center",formatter:DatFormatter}, 
					             {label:"状态",name : 'menuStatus',index : 'menu_status',width : 60,align : "center", editable: true,edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.useStatus)}}
					           ],
		            shrinkToFit:false,   
		            autoScroll: true, 
					rowNum : 10,//一页显示多少条
					rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
					rownumWidth: 20, //设置显示序号的宽度，默认为25  
					pager : '#menuGridPager',//表格页脚的占位符(一般是div)的id
					sortname : 'index_Sort',//初始化的时候排序的字段
					sortorder : "asc",//排序方式,可选desc,asc
					mtype : "post",//向后台请求数据的ajax的类型。可选post,get
					width:page.window.grid.width-200,
					height:page.window.grid.halfHeight,
					onSelectRow:function(rowId){
						GirdCtr.event.selectRow(_this,rowId);
					}
				});
		/*创建jqGrid的操作按钮容器*/
		/*可以控制界面上增删改查的按钮是否显示*/
		$("#menuGrid").jqGrid('navGrid', '#menuGridPager', {edit:false,del:false,add:false,search:false,refresh:false});
	},
	buttonBind:function(){//控制按钮初始化
	  var _this = this;
	  $("#addMenuBtn").bind("click",function(){_this.event.doAdd(_this,this)});
	  $("#editMenuBtn").bind("click",function(){_this.event.doEdit(_this,this)});
	  $("#deleteMenuBtn").bind("click",function(){_this.event.doDelete(_this,this)});
	  $("#detailMenuBtn").bind("click",function(){_this.event.doDetail(_this,this)});
	},
	event:{//事件
	  doAdd:function(_this,btn){
		  var menuId = $(btn).attr("menuId");
		  var node = TreeCtr.getSelectNode();
		  if(node){
			  var params = {menuId:menuId,parentMenuId:node.id,parentMenuName:node.name}
			  showWindow("添加菜单",contextPath+"/system/resources/menuAddView.htm",params,function(){
				  GirdCtr.reLoad();
				  TreeCtr.reloadSelectNode();
			  });
		  }else{
			  layer.msg("请选择一个树节点");
		  }
	  },
	  doEdit:function(_this,btn){//修改
		  var menuId = $(btn).attr("menuId");
		  var node = GirdCtr.getSelectRowData();
		  if(node){
			  var params = {menuId:menuId,id:node.menuId,parentMenuId:node.parentMenuId}
			  showWindow("修改菜单",contextPath+"/system/resources/menuUpdateView.htm",params,function(){
				  GirdCtr.reLoad();
				  TreeCtr.reloadSelectNode();
			  });
		  }else{
			  layer.msg("选择需要修改的数据");
		  }
	  },
	  doDetail:function(_this,btn){//修改
		  var menuId = $(btn).attr("menuId");
		  var node = GirdCtr.getSelectRowData();
		  if(node){
			  var params = {menuId:menuId,id:node.menuId}
			  showWindow("查看组织",contextPath+"/system/resources/menuDetailView.htm",params,function(){
				  
			  });
		  }else{
			  layer.msg("选择需要查看的数据");
		  }
	  },
	  doDelete:function(_this,btn){//删除
		  var id = _this.grid.jqGrid('getGridParam','selrow');
		  if(id){
			 var laIndex = layer.confirm('确定要删除该数据吗？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
					_this.event.gridDoDelete(id,_this);
					layer.close(laIndex);
				});
		  }else{
			  layer.msg("请选择删除的数据.");
		  }
	  },gridDoDelete:function(id,_this){
		  $.dataPost(contextPath+'/system/resources/menuDelete.htm',{id:id},function(result){
			  if(result.success){
				  GirdCtr.reLoad();
				  TreeCtr.reloadSelectNode();
			  }else{
				  layer.msg(result.msg); 
			  }
		  });
	  },selectRow:function(_this,rowId){
		 var rowData = _this.grid.getRowData(rowId);
		 ButtonGirdCtr.gridQuery({menuId:rowData.menuId})
	  }
	}
});


var ButtonGirdCtr = $.extend({},defaultGridConFig,{
		   gridInit: function(){
			 var _this = this;
			//创建jqGrid组件
			_this.grid = $("#buttonGrid").jqGrid({
				 			datatype: "json",			
				 			url : contextPath+'/system/resources/buttonQuery.htm',//组件创建完成之后请求数据的url
							datatype : "json",//请求数据返回的类型。可选json,xml,txt
							colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
							             {label:"主键", name : "buttonId",index:"button_Id",hidden:true,key:true},
							             {label:"按钮名称", name : 'buttonName',index : 'button_Name',width : 140,align : "center"}, 
							            // {label:"所属菜单", name : 'menuId',index : 'menu_id',width : 140,align : "center",}, 
							             {label:"按钮样式", name : 'icon',index : 'icon',width : 80,align : "center"}, 
							             {label:"按钮编号", name : 'buttonCode',index : 'button_code',width : 80,align : "center"}, 
							             {label:"类型", name : 'buttonType',index : 'button_Type',width : 80,align : "center",edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.buttonType)}}, 
							             {label:"分组", name : 'groupName',index : 'group_name',width : 80,align : "center"}, 
							             {label:"路径", name : 'url',index : 'url',width : 60,align : "center"}, 
							             {label:"权限路径", name : 'authUrl',index : 'auth_Url',width : 200,align : "center"}, 
							             {label:"排序", name : 'indexSort',index : 'index_Sort',width : 50,align : "center"},
							             {label:"创建时间", name : 'createTime',index : 'create_Time',width : 130,align : "center",formatter:DatFormatter}, 
							             {label:"状态",name : 'buttonStatus',index : 'button_Status',width : 60,align : "center", editable: true,edittype:"select",formatter:'select', editoptions:{value:systemDict.toString(systemDict.useStatus)}}
							           ],
				            shrinkToFit:false,   
				            autoScroll: true, 
							rowNum : 10,//一页显示多少条
							rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
							rownumWidth: 20, //设置显示序号的宽度，默认为25  
							pager : '#buttonGridPager',//表格页脚的占位符(一般是div)的id
							sortname : 'group_name',//初始化的时候排序的字段
							sortorder : "asc",//排序方式,可选desc,asc
							mtype : "post",//向后台请求数据的ajax的类型。可选post,get
							width:page.window.grid.width-200,
							height:page.window.grid.halfHeight
						});
				/*创建jqGrid的操作按钮容器*/
				/*可以控制界面上增删改查的按钮是否显示*/
				$("#buttonGrid").jqGrid('navGrid', '#buttonGridPager', {edit:false,del:false,add:false,search:false,refresh:false});
			},
			buttonBind:function(){//控制按钮初始化
			  var _this = this;
			  $("#addButtonBtn").bind("click",function(){_this.event.doAdd(_this,this)});
			  $("#editButtonBtn").bind("click",function(){_this.event.doEdit(_this,this)});
			  $("#deleteButtonBtn").bind("click",function(){_this.event.doDelete(_this,this)});
			  $("#detailButtonBtn").bind("click",function(){_this.event.doDetail(_this,this)});
			},
			event:{//事件
			  doAdd:function(_this,btn){
				  var menuId = $(btn).attr("menuId");
				  var node = GirdCtr.getSelectRowData();
				  if(node){
					  var params = {mId:menuId,menuId:node.menuId,menuName:node.menuName};
					  showWindow("添加按钮",contextPath+"/system/resources/buttonAddView.htm",params,function(){
						  ButtonGirdCtr.reLoad();
					  });
				  }else{
					  layer.msg("请选择菜单.");
				  }
			  },
			  doEdit:function(_this,btn){//修改
				  var id = _this.grid.jqGrid('getGridParam','selrow');
		          if(id){
		        	  var menuId = $(btn).attr("menuId");
		    		  var node = ButtonGirdCtr.getSelectRowData();
		    		  if(node){
		    			  var params = {menuId:menuId,id:node.buttonId}
		    			  showWindow("修改按钮",contextPath+"/system/resources/buttonUpdateView.htm",params,function(){
		    				  ButtonGirdCtr.reLoad();
		    			  });
		    		  }else{
		    			  layer.msg("选择需要修改的数据");
		    		  }
				  }else{
					  layer.msg("请选择修改的数据.");
				  }
			  },
			  doDetail:function(_this,btn){//修改
				  var menuId = $(btn).attr("menuId");
				  var node = GirdCtr.getSelectRowData();
				  if(node){
					  var params = {menuId:menuId,id:node.buttonId}
					  showWindow("查看组织",contextPath+"/system/resources/buttonDetailView.htm",params,function(){
						  
					  });
				  }else{
					  layer.msg("选择需要查看的数据");
				  }
			  },
			  doDelete:function(_this){//删除
				  var id = _this.grid.jqGrid('getGridParam','selrow');
				  if(id){
					 var laIndex = layer.confirm('确定要删除该数据吗？', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							_this.event.gridDoDelete(id,_this);
							layer.close(laIndex);
						});
				  }else{
					  layer.msg("请选择删除的数据.");
				  }
			  },gridDoDelete:function(id,_this){
				  $.dataPost(contextPath+'/system/resources/buttonDelete.htm',{id:id},function(result){
					  if(result.success){
						  ButtonGirdCtr.reLoad();
					  }else{
						  layer.msg(result.msg); 
					  }
				  });
			  }
			}
});




